from race.Player import *
import logging.config
from wowThread.threadJob import *
from pynput.keyboard import Key

logging.config.fileConfig('./util/logging.conf')
log = logging.getLogger("example")

"""
    子类描述：
        1、lxr坦克类
"""


class DragonHealer(Player):

    def on_keyboard_press_son_before_thread(self, key):
        log.debug("on_keyboard_press_son_before_thread 1:进入了函数")
        # 有些按键是直接终止别人的，继续自己的，有些不是这样的，因此要区别对待【排列组合，情况非常多】/【也要超级符合每个职业的操作习惯】
        if key in (
                Key.f1, Key.f2, Key.f3, Key.f4, Key.f5, Key.f6, Key.f7, Key.f8, Key.f9, Key.f10, Key.f11,
                Key.f12):
            pass
        elif key == Key.enter:
            pass
        elif key == Key.space:
            pass
        elif key == Key.alt_l:
            pass
        elif key == Key.ctrl_l:
            pass
        elif key == Key.caps_lock:
            pass
        elif key == Key.shift_l:
            pass
        elif key.char == 'a':
            # 不终止别人，后面让只允许一个线程执行的逻辑来顺序排
            # ★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|
            # 并行并发执行的折中方案【通过触发其他按键，这样的好处是线程池统一管理了】
            pass
        elif key.char == 'z':
            pass
        elif key.char == 'q':
            pass
        elif key.char == 'w':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'r':
            # 终止掉别人
            # interruptAllByForce()
            pass
        elif key.char == 'g':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'x':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'v':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'h':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'c':
            # 终止掉别人
            self.interruptAllByForce()
        elif key.char == 'e':
            pass
        elif key.char == 's':
            pass
        elif key.char == 'd':
            pass
        elif key.char == 'f':
            pass
        elif key.char == 'b':
            pass
        return

    # 执行父类的方法
    # @logAop
    def a(self):
        # 父类的方法先执行（或者后续看看是不是就直接用之类覆盖之后的方法）
        self.startScheduler1()  # 调用 CallBackScheduler1
        #self.startScheduler2()  # 调用 CallBackScheduler2
        #self.startScheduler3()  # 调用 CallBackScheduler3
        #self.startScheduler4()  # 调用 CallBackScheduler3
        return

    def mouseUp(self, x, y):
        # self.startScheduler3()
        while self.ui.stopLoop == False:
            # # 群体加血
            # self.click(self.kb, '9', 3)
            # sleep(0.2)
            # self.click(self.kb, '0', 3)
            # sleep(0.2)
            # self.click(self.kb, '8', 3)
            pass
            sleep(3)

    def mouseDown(self, x, y):
        log.debug('在神牧中的mouseDown1')
        print('在神牧中的mouseDown2:' + str(self.ui.stopLoop))
        self.startScheduler2()
        self.startScheduler3()
        # self.startScheduler4()
        return

    def CallBackScheduler1(self):
        while self.ui.stopLoop == False:
            self.click(self.kb, 'm', 3)
            sleep(0.1)
            self.click(self.kb, '=', 2)
            sleep(0.1)
            self.click(self.kb, 'b', 2)
            sleep(0.1)
            self.click(self.kb, '-', 2)
            sleep(0.1)
            self.click(self.kb, '`', 2)

    def CallBackScheduler2(self):
        while self.ui.stopLoop == False:
            # 滚轮上线不能用self.ui.stopLoop == False，这个，因为键盘按z键后，这个值就变成True了，就无法进行循环了
            # currentMousePosition[0]:x|currentMousePosition[1]:y
            currentMousePosition = win32api.GetCursorPos()
            x1 = int(getIni("./util", "heal5bar", "x1"))
            y1 = int(getIni("./util", "heal5bar", "y1"))
            y1CurrentMouse = int(currentMousePosition[1]) - 5
            x2 = int(getIni("./util", "heal5bar", "x2"))
            y2 = int(getIni("./util", "heal5bar", "y2"))
            y2CurrentMouse = int(currentMousePosition[1]) + 5
            HStep = (y2-y1)/5
            HMouse = 10
            width = x2 - x1
            # imButton1 = Image.open(
            #     r'E:\game\World of Warcraft\_retail_\Screenshots\WoWScrnShot_102122_201804.jpg')
            allBarsRoi = ImageGrab.grab(
                bbox=(x1, y1CurrentMouse, x2, y2CurrentMouse))  # X1,Y1,X2,Y2
            all5BarsRoi = ImageGrab.grab(
                bbox=(x1, y1, x2, y2))  # X1,Y1,X2,Y2
            # allBarsRoi = imButton1.crop((x1, y1, x2, y2))
            # debug
            # cv2.imshow('debug', np.array(allBarsRoi))
            # cv2.waitKey(0)
            # cv2.imwrite('ms.jpg',np.array(allBarsRoi))
            # rowsNum = 0.5
            # currentMouseBar = allBarsRoi.crop(
            #     (0, HStep*rowsNum-HMouse/2, width, HStep*rowsNum+HMouse/2))
            # print("ImageGrab图像抓取，像素识别前时分秒:" +
            #       datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
            iRestHP = healerCheckOtherBloodBarInGrid(
                currentMouseBar=allBarsRoi, HMouse=HMouse, width=width)
            dfdangers = PersonNBloods(
                personNum=5, all5BarsRoi=all5BarsRoi, HMouse=HMouse, width=width, HStep=HStep)
            print('antasya(s):' + str(iRestHP))
            avgNumber = int(dfdangers[dfdangers['type']
                                  == 'avgNumber']['numbers'].values[0])
            danger90 = int(dfdangers[dfdangers['type']
                                 == 'danger90']['numbers'].values[0])
            danger80 = int(dfdangers[dfdangers['type']
                                 == 'danger80']['numbers'].values[0])
            danger70 = int(dfdangers[dfdangers['type']
                                 == 'danger70']['numbers'].values[0])
            danger50 = int(dfdangers[dfdangers['type']
                                 == 'danger50']['numbers'].values[0])
            danger40 = int(dfdangers[dfdangers['type']
                                 == 'danger40']['numbers'].values[0])
            print('openlabopenlab(avgNumber):' + str(avgNumber))
            if avgNumber <= 50:
                # 群体减伤+顺发
                self.click(self.kb, 'k', 6)
            if avgNumber <= 65:
                # 群体减伤+顺发
                self.click(self.kb, 'l', 6)
            if avgNumber <= 70:
                # 群体减伤+顺发
                self.click(self.kb, 'p', 6)
            if avgNumber <= 75:
                # 群体减伤+顺发
                self.click(self.kb, 'i', 6)
            # or danger80 >= 3 or danger70 >= 2 danger90 >= 4 avgNumber <= 80
            if danger80 >= 3 or danger70 >= 2 or danger90 >= 4 or avgNumber <= 80:
                # ###############################群体加血
                # 群体加血
                self.click(self.kb, '9', 5)
                self.pubAvgColorTiggerKeyBloodBarMode(self.WACastBarX + self.WACastBarWidth * 0.65, self.WACastBarY,
                                                  self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                  self.WABloodBarAvgColor, self.GREEN, 'small', '0', 0.06, 3, self.ELSEKEY_9, self.DEBUG_CLOSE)
                self.pubAvgColorTiggerKeyBloodBarMode(self.WACastBarX + self.WACastBarWidth * 0.65, self.WACastBarY,
                                                  self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                  self.WABloodBarAvgColor, self.GREEN, 'small', '8', 0.06, 3, self.ELSEKEY_9, self.DEBUG_CLOSE)
                if iRestHP <= 35:
                    pass
                    # self.click(self.kb, Key.f3, 3)
                elif iRestHP > 35 and iRestHP <= 90:
                    # 相当于树皮
                    self.click(self.kb, 'n', 4)
                elif iRestHP > 90:
                    # 龙希尔蓄力条
                    pass
                # 龙希尔蓄力条
                self.pubAvgColorTiggerKeyBloodBarMode(self.WACastBarX + self.WACastBarWidth * 0.65, self.WACastBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', '2', 0.06, 5, self.ELSEKEY_9, self.DEBUG_CLOSE)
                # 龙希尔蓄力条
                self.pubAvgColorTiggerKeyBloodBarMode(self.WACastBarX + self.WACastBarWidth * 0.65, self.WACastBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', '1', 0.06, 4, self.ELSEKEY_9, self.DEBUG_CLOSE)
                # 龙希尔蓄力条
                self.pubAvgColorTiggerKeyBloodBarMode(self.WACastBarX + self.WACastBarWidth * 0.65, self.WACastBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', '3', 0.06, 3, self.ELSEKEY_9, self.DEBUG_CLOSE)
            else:
                # ###############################单体加血
                # 总会执行到，所有的条件都会执行到
                self.click(self.kb, '2', 6)
                self.click(self.kb, '1', 6)
                self.click(self.kb, '3', 3)
                
                if iRestHP <= 35:
                    # self.click(self.kb, 't', 3)
                    pass
                elif iRestHP > 35 and iRestHP < 60:
                    # 相当于树皮
                    self.click(self.kb, 'n', 4)
                elif iRestHP > 60 and iRestHP < 70:
                    # 相当于树皮
                    self.click(self.kb, 'n', 4)
                elif iRestHP > 70 and iRestHP < 80:
                    pass
                elif iRestHP > 80:
                    pass

    def CallBackScheduler3(self):
        while self.ui.stopLoop == False:
            # 自己给自己加血
            self.pubAvgColorTiggerKeyBloodBarMode(self.WABloodBarX + self.WABloodBarWidth * 0.85, self.WABloodBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', '7', 0.06, 5, self.ELSEKEY_NONE, self.DEBUG_CLOSE)

            # 自己给自己减伤
            self.pubAvgColorTiggerKeyBloodBarMode(self.WABloodBarX + self.WABloodBarWidth * 0.7, self.WABloodBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', '\\', 0.06, 4, self.ELSEKEY_NONE, self.DEBUG_CLOSE)

            # 自己给自己减伤
            self.pubAvgColorTiggerKeyBloodBarMode(self.WABloodBarX + self.WABloodBarWidth * 0.5, self.WABloodBarY,
                                                self.WABloodBarDeltaX, self.WABloodBarDeltaY,
                                                self.WABloodBarAvgColor, self.GREEN, 'small', 't', 0.06, 4, self.ELSEKEY_NONE, self.DEBUG_CLOSE)
            sleep(0.2)

    def CallBackScheduler4(self):
        while self.ui.stopLoop == False:
            # 免费的快速治疗，先用掉
            self.pubAvgColorTiggerKeyBuffMode(self.WA_SQUARE_ZFX_X1, self.WA_SQUARE_ZFX, self.WABloodBarDeltaX,
                                              self.WABloodBarDeltaY,
                                              self.WA_SQUARE_ZFX_AvgColor,
                                              self.RED, 'big', '6', 0.06, 3, self.ELSEKEY_5 ,self.DEBUG_CLOSE)
            sleep(0.3)

    def on_mouse_scroll_son_before_thread(self, dx, dy):
        # 让鼠标上下切换比较自由方便，中间不需要按Z键进行中转
        if dy < 0:
            # 滚轮向下
            self.interruptAllByForceMouseUpDown()  # 强制打断正在运行的循环
        elif dy > 0:
            # 滚轮向上
            self.interruptAllByForceMouseUpDown()  # 强制打断正在运行的循环
        return
